﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using Tools;

namespace Dialogs
{
    [CommandAttribute(true)]
    public partial class create_message_type : BaseForm, ITsqlCommand
    {
        public create_message_type()
            : base()
        {

        }

        public create_message_type(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbSchemas.DataSource = Utility.schema_collections(dbContext);
            cbSchemas.DisplayMember = "Name";
            cbSchemas.ValueMember = "schemat";

            cbAuthorization.DataSource = Utility.authorizations(dbContext);
            cbAuthorization.DisplayMember = "Name";

        }

        private void chAuthorization_CheckedChanged(object sender, EventArgs e)
        {
            cbAuthorization.Enabled = chAuthorization.Checked;

            Write();
        }

        private void chValidation_CheckedChanged(object sender, EventArgs e)
        {
            cbValidation.Enabled = chValidation.Checked;

            Write();
        }

        private void cbValidation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((string)cbValidation.SelectedItem == "VALID_XML WITH SCHEMA COLLECTION")
            {
                cbSchemas.Enabled = true;
            }
            else cbSchemas.Enabled = false;

            Write();
        }

        private void cbAuthorization_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbSchemas_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE [" + dbContext.Database + "];");
            sb.AppendLine();
            sb.AppendLine("CREATE MESSAGE TYPE [" + tbName.Text + "]");

            if (chAuthorization.Checked)
            {
                sb.AppendLine("AUTHORIZATION [" + cbAuthorization.Text + "]");
            }

            if (chValidation.Checked)
            {
                sb.AppendLine("VALIDATION = " + cbValidation.Text);
            }

            if ((string)cbValidation.SelectedItem == "VALID_XML WITH SCHEMA COLLECTION")
            {
                sb.AppendLine("   [" + cbSchemas.Text + "]");
            }

            command = sb.ToString();

            tbCommand.Text = command;
        }

        private string command;

        public string Command
        {
            get
            {
                return command;
            }
        }

        private void btnSchow_Click(object sender, EventArgs e)
        {
            if (cbSchemas.Text == string.Empty)
            {
                return;
            }
        }
    }
}